<?php
/**
 * gpa_new Description
 * @package
 * @name admin_gallery
 * @filesource admin_gallery.php
 * @author Meta Sanjaya
 */

class admin_user extends Controller
{
    public $config = array();
    /**
     * @access public
     * @method __construct
     * @param void
     * @return void
     */
    public function  __construct()
    {
        parent::__construct();
        getInstance('config')->retrieve('user');
        foreach(getInstance('config')->getAll() as $key => $configs) {
            if($key=='user') {
                foreach($configs as $name => $value) {
                    $this->config[$name] = $value;
                }
            }
        }
    }

    public function conf($param) {
        $post = getInstance("request")->post;
        $config =& getInstance("config");

        if(isset($post["submit_config"])) {
            unset($post["submit_config"]);
            foreach($post as $name => $value) {
                $config->set($name, $value, "user");
            }
        }

        redirect(getAppUrl('admin', 'control') . '/app/user', false);
    }

    public function edit($param) {
        $post = getInstance("request")->post;

        $model = $this->getModel("user");

        switch($post['name']) {
            case "active_user":
                if(intval($post['checked'])) {
                    if($model->activateUser($post['id'])) {
                        die("data changed!");
                    } else {
                        die("error!");
                    }
                } else {
                    if($model->deactivateUser($post['id'])) {
                        die("data changed!");
                    } else {
                        die("error!");
                    }
                }
            break;
            case "verified_user":
                if(intval($post['checked'])) {
                    if($model->verifyUser($post['id'])) {
                        die("data changed!");
                    } else {
                        die("error!");
                    }
                } else {
                    if($model->unverifyUser($post['id'])) {
                        die("data changed!");
                    } else {
                        die("error!");
                    }
                }
            break;
        }

        die("unknown error!");
    }

    public function __default($param)
    {
        $view = 'admin_user_html';
        $model = $this->getModel('user');
        $var['page_title'] = 'Users Control';
        $var['config'] = $this->config;
        $var['apps'] = getApps();
        $var['users'] = $model->getUsers();
        $this->addVars($var);
        $this->renderView($view, $param['app']);
    }
}
?>
